package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class IndexController {
    @Resource
    private IndexService indexService;
    @GetMapping("/**")
    public String toIndex(Model model,@RequestHeader(value = "userId",required = false)String userId){
        System.out.println("================" + userId);
        //加载一级分类的数据
        List<CategoryEntity> categoryEntities = indexService.queryLevelCategories();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @GetMapping("index/cates/{pid}")
    @ResponseBody
    public ResponseVo<List<CategoryEntity>> querySubsCategoriesBuPid(@PathVariable Long pid){
        //展示二三级分类
        List<CategoryEntity> categoryEntityList = indexService.querySubsCategoriesBuPid(pid);
        return ResponseVo.ok(categoryEntityList);
    }

    @GetMapping("index/test/lock")
    @ResponseBody
    public ResponseVo test(){
        indexService.test();
        return ResponseVo.ok();
    }
}
